﻿using Microsoft.AspNetCore.Mvc.Controllers;

namespace ApiLib.Common.Attributes.IocAttributes;

/// <summary>
/// 缓存
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class CacheableAttribute : Attribute
{
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 过期时间（秒）
    /// </summary>
    public int Expiration { get; set; }

    /// <summary>
    /// 获取缓存名称
    /// </summary>
    /// <param name="descriptor"></param>
    /// <returns></returns>
    public string GetCacheName(ControllerActionDescriptor descriptor)
    {
        return "ac:" + (string.IsNullOrEmpty(Name) ? (descriptor.ControllerName + "." + descriptor.ActionName) : Name);
    }
}